package com.company.dcm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.dcm.common.R;
import com.company.dcm.entity.Ota;
import com.company.dcm.mapper.OtaMapper;
import com.company.dcm.service.OTAService;
import com.company.dcm.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class OTAServiceImpl implements OTAService {
    @Autowired
    OtaMapper otaMapper;

    @Autowired
    private TaskService taskService;

    @Async
    @Override
    public void upload(MultipartFile otaFile, String otaFilePath, String hardwareVersion,String softwareVersion,String productType,String taskId) throws IOException, InterruptedException {
        Ota ota = new Ota();

        ota.setHardwareVersion(hardwareVersion);
        ota.setSoftwareVersion(softwareVersion);
        ota.setProductType(productType);
        ota.setOtaFileName(otaFile.getOriginalFilename());

        String a = "test";

        try {
            // 第一阶段：数据库操作
            otaMapper.insert(ota);
            taskService.updateProgress(taskId, 0.7F);

        } catch (Exception e) {
            taskService.updateProgress(taskId, -1F); // 失败状态
            throw new RuntimeException("上传失败", e);
        }finally {
            taskService.removeTask(taskId);
        }
    }

    @Override
    public boolean duplicate(MultipartFile otaFile, String otaFilePath, String hardwareVersion, String softwareVersion, String productType) throws IOException {
        Ota ota = new Ota();

        ota.setHardwareVersion(hardwareVersion);
        ota.setSoftwareVersion(softwareVersion);
        ota.setProductType(productType);
        ota.setOtaFileName(otaFile.getOriginalFilename());

        HashMap<String,Object> map = new HashMap<>();
        //自定义要查询
        map.put("hardware_version",ota.getHardwareVersion());
        map.put("software_version",ota.getSoftwareVersion());
        map.put("product_type",ota.getProductType());

        List<Ota> otas = otaMapper.selectByMap(map);

        return otas.isEmpty();
    }


    @Override
    public List<Ota> getOTAList(String product_type) {
        QueryWrapper<Ota> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_type", product_type); // 字段名, 属性值
        List<Ota> otas = otaMapper.selectList(queryWrapper);

        return otas;
    }

    @Override
    public List<Object> getProductType() {
        QueryWrapper<Ota> wrapper = new QueryWrapper<Ota>()
                .select("DISTINCT product_type").isNotNull("product_type"); // 核心语法

        // 方式1.1：获取Object列表（适用于单个字段）
        List<Object> types = otaMapper.selectObjs(wrapper);


        return types;
    }

    @Override
    public boolean deleteOta(String otaId) {

        int i = otaMapper.deleteById(otaId);

        if (i == 1){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public boolean updateOta(Integer otaId,MultipartFile otaFile, String otaFilePath, String hardwareVersion,String softwareVersion,String productType) throws IOException {

        Ota ota = new Ota();
        ota.setOtaId(otaId);


        ota.setHardwareVersion(hardwareVersion);
        ota.setSoftwareVersion(softwareVersion);
        ota.setProductType(productType);

        if (!(otaFile == null)){
            ota.setOtaFileName(otaFile.getOriginalFilename());
            FileUtils.saveToTemp(otaFile);
        }

        int i = otaMapper.updateById(ota);

        if (i == 1){
            return true;
        }else {
            return false;
        }
    }
}
